package registry;

import java.net.ServerSocket;
import java.net.Socket;

class Daemon {

	int port;						// port to listen to
	Socket connection;				
	ServerSocket listenSocket;		// Server socket to accept incoming connection requests
	
	
	/**
	 * Parameterized constructor
	 * @param port
	 * @param m
	 */
	Daemon ( int port)  {
    
        this.port = port;
        try  {
			listenSocket = new ServerSocket ( port );
            //debug
            System.out.println ( "Server started:\n  listening on port " + port + "\n\n" );
            
            // continuous loop to listen to incoming requests and spawn a new thread everytime
            // to handle the incoming request
            while ( true )  {
                Socket connection = listenSocket.accept();
                System.out.println ("got connection");
                DaemonThread receiveMessage = new DaemonThread (connection);
              //  DaemonThread.start ();
            }

    }  catch ( Exception e )  {
        e.printStackTrace ();
        System.exit ( 1 );
    }
    }

    
    public static void main(String []args) {
    	Daemon d = new Daemon(3333);
    }
}

